﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils;
using Model;
using Dao;
using System.Data;

namespace Website.uc
{
    public partial class ucRegister : System.Web.UI.UserControl
    {
        UserDao userDao = new UserDao();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if(CheckValidate.CheckNull(txtUsername.Text))
            {
                lblMessage.Text = "Enter username !!";
                txtUsername.Focus();
            }
            else if (CheckValidate.CheckNull(txtPassword.Text))
            {
                lblMessage.Text = "Enter password !!";
                txtPassword.Focus();
            }
            else if (CheckValidate.CheckNull(txtConfirm.Text))
            {
                lblMessage.Text = "Enter confirm !!";
                txtConfirm.Focus();
            }
            else if (!txtPassword.Text.Equals(txtConfirm.Text))
            {
                lblMessage.Text = "Password not equal !!";
                txtConfirm.Focus();
            }
            else if (CheckValidate.CheckNull(txtEmail.Text))
            {
                lblMessage.Text = "Enter email !!";
                txtEmail.Focus();
            }
            else if (!CheckValidate.CheckEmail(txtEmail.Text))
            {
                lblMessage.Text = "Enter email invalid !!";
                txtEmail.Focus();
            }
            else
            {
                DataTable tb = userDao.getUserByUserName(txtUsername.Text);
                if (tb.Rows.Count > 0)
                {
                    Utility.ShowMessage("This username has existed !!", Page);
                }
                else
                {
                    User user = new User();
                    user.UserName = txtUsername.Text;
                    user.Password = txtPassword.Text;
                    user.Email = txtEmail.Text;
                    user.Status = "0";
                    user.UserType = "user";

                    userDao.insertUser(user);
                    Utility.ShowMessage("Register successfull !!", Page);
                    reset();
                }

            }
        }

        private void reset()
        {
            txtConfirm.Text = "";
            txtEmail.Text = "";
            txtPassword.Text = "";
            txtUsername.Text = "";
            lblMessage.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            reset();
        }
    }
}